{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #How Many Numbers Are Smaller Than the Current Number"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #counting #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #计数 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: smallerNumbersThanCurrent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #有多少小于当前数字的数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个数组&nbsp;<code>nums</code>，对于其中每个元素&nbsp;<code>nums[i]</code>，请你统计数组中比它小的所有数字的数目。</p>\n",
    "\n",
    "<p>换而言之，对于每个&nbsp;<code>nums[i]</code>&nbsp;你必须计算出有效的&nbsp;<code>j</code>&nbsp;的数量，其中 <code>j</code> 满足&nbsp;<code>j != i</code> <strong>且</strong> <code>nums[j] &lt; nums[i]</code>&nbsp;。</p>\n",
    "\n",
    "<p>以数组形式返回答案。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [8,1,2,2,3]\n",
    "<strong>输出：</strong>[4,0,1,1,3]\n",
    "<strong>解释：</strong> \n",
    "对于 nums[0]=8 存在四个比它小的数字：（1，2，2 和 3）。 \n",
    "对于 nums[1]=1 不存在比它小的数字。\n",
    "对于 nums[2]=2 存在一个比它小的数字：（1）。 \n",
    "对于 nums[3]=2 存在一个比它小的数字：（1）。 \n",
    "对于 nums[4]=3 存在三个比它小的数字：（1，2 和 2）。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [6,5,4,8]\n",
    "<strong>输出：</strong>[2,1,0,3]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [7,7,7,7]\n",
    "<strong>输出：</strong>[0,0,0,0]\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 500</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [how-many-numbers-are-smaller-than-the-current-number](https://leetcode.cn/problems/how-many-numbers-are-smaller-than-the-current-number/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [how-many-numbers-are-smaller-than-the-current-number](https://leetcode.cn/problems/how-many-numbers-are-smaller-than-the-current-number/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[8,1,2,2,3]', '[6,5,4,8]', '[7,7,7,7]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        i = 0\n",
    "        ans = []\n",
    "        k = 0\n",
    "        while i < len(nums):\n",
    "            for j in range(len(nums)):\n",
    "                if nums[i] > nums[j] and j != i:\n",
    "                    k += 1\n",
    "            ans.append(k)\n",
    "            i += 1\n",
    "            k = 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        new = sorted(nums)\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            res.append(new.index(num))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 1. 解法一: 暴力解法\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        res = [0] * n\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if nums[j] < nums[i]:\n",
    "                    res[i] += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        cnt = [0] * len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                if nums[j]<nums[i] and j!=i:\n",
    "                    cnt[i] += 1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\r\n",
    "        sorted_nums = sorted(nums)\r\n",
    "        res = []\r\n",
    "        dic = {}\r\n",
    "        for i,n in enumerate(sorted_nums):\r\n",
    "            if i > 0 and sorted_nums[i] == sorted_nums[i-1]:\r\n",
    "                dic[n] = dic[sorted_nums[i-1]]\r\n",
    "            else:\r\n",
    "                dic[n] = i\r\n",
    "        for num in nums:\r\n",
    "            res.append(dic[num])\r\n",
    "        return res\r\n",
    "            \r\n",
    "            \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        return [sum(i < n for i in nums) for n in nums]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        nums1 = nums.copy()\n",
    "        nums1.sort()\n",
    "        anwser = []\n",
    "        for i in nums:\n",
    "            anwser.append(nums1.index(i))\n",
    "        return anwser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        \n",
    "        return [len([x for x in nums if x<i]) for i in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        new_nums = sorted(nums)\n",
    "        ans = []\n",
    "        for num in nums:\n",
    "            ans.append(new_nums.index(num))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        for i in range(len(nums)):\n",
    "            count = 0\n",
    "            for j in range(len(nums)):\n",
    "                if nums[j] < nums[i]:\n",
    "                    count += 1\n",
    "            result.append(count)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        abc=[0]*101\n",
    "        for i in nums:\n",
    "            abc[i]+=1\n",
    "        ans=[]\n",
    "        temp=0\n",
    "        s=[]\n",
    "        for i in abc:\n",
    "            s.append(temp)\n",
    "            temp+=i\n",
    "        for j in nums:\n",
    "            ans.append(s[j])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        count = 0\n",
    "        ans = []\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                if nums[i] > nums[j]:\n",
    "                    count += 1\n",
    "            ans.append(count)\n",
    "            count =0\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        \n",
    "        n1 = nums[:]\n",
    "        n1.sort()\n",
    "        dic = {}\n",
    "        for i in range(len(n1)):\n",
    "            if n1[i] not in dic:\n",
    "                dic[n1[i]] = i\n",
    "        return [dic[i] for i in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        # 两层 for 循环，时间复杂度为 O(N^2)\n",
    "        # 优化：1. 先排序，2. 用哈希表记录每个元素对应的下表。对于相同的元素，只记录最左边的下标，因此若当前元素已经在哈希表中被记录过了，则跳过。\n",
    "        # 优化后：先排序再遍历，时间复杂度为 O(nlogn)\n",
    "        res = nums[:]\n",
    "        hash = dict()\n",
    "        res.sort() # 从小到大排序之后，元素下标就是小于当前数字的数字\n",
    "        for i, num in enumerate(res):\n",
    "            if num  not in hash.keys(): # 遇到了相同的数字，那么不需要更新该 number 的情况\n",
    "                hash[num] = i       \n",
    "        for i, num in enumerate(nums):\n",
    "            res[i] = hash[num]\n",
    "        return res\n",
    "        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        s = sorted(nums)\n",
    "        arr = []\n",
    "        for x in nums:\n",
    "            arr.append(s.index(x))\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n=len(nums)\n",
    "        result=[]\n",
    "        for i in range(n):\n",
    "            count=0\n",
    "            for j in range(n):\n",
    "                if i !=j and nums[i]>nums[j]:\n",
    "                    count +=1\n",
    "            result.append(count)\n",
    "        return result\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        s = sorted(nums)\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = s.index(nums[i])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        umap = {}\n",
    "        vec = sorted(nums, reverse=True)\n",
    "        for i in range(len(nums)):\n",
    "            umap[vec[i]] = len(nums) - i - 1\n",
    "        ans = [0] * len(nums)\n",
    "        for i in range(len(ans)):\n",
    "            ans[i] = umap[nums[i]]\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        l = len(nums)\n",
    "        result = l * [0]\n",
    "        for i in range(l):\n",
    "            for j in range(l):\n",
    "                if i == j:\n",
    "                    continue\n",
    "                elif nums[i] > nums[j]:\n",
    "                    result[i] += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        num = sorted(nums)\n",
    "        return [num.index(i) for i in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        small_num = []\n",
    "        nums1 = copy.deepcopy(nums)\n",
    "        for i in range(len(nums)):\n",
    "            small = 0\n",
    "            for j in range(len(nums1)):\n",
    "                if nums[i] > nums1[j]:\n",
    "                    small += 1              \n",
    "            small_num.append(small)\n",
    "        return small_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        nums1 = nums[:]\n",
    "        nums1.sort()\n",
    "        dic = {}\n",
    "        for i in range(len(nums1)):\n",
    "            if nums1[i] not in  dic:\n",
    "                dic[nums1[i]] = i\n",
    "        res = [0] * len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            res[i] = dic[nums[i]]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        arr = [0] * n\n",
    "        for i in range(n):\n",
    "            ans = 0\n",
    "            for j in range(n):\n",
    "                if i == j:\n",
    "                    continue\n",
    "                if nums[i] > nums[j]:\n",
    "                    ans += 1\n",
    "            arr[i] = ans\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        for i in range(len(nums)):\n",
    "            count = 0\n",
    "            for j in range(len(nums)):\n",
    "                if nums[j] < nums[i]:\n",
    "                    count += 1\n",
    "            result.append(count)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        for i in range(0,len(nums)):\n",
    "            smaller = 0\n",
    "            for j in range(0,len(nums)):\n",
    "                if j != i and nums[j] < nums[i]:\n",
    "                    smaller += 1\n",
    "            result.append(smaller)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        cnt = [0 for _ in range(101)]\n",
    "        hash_T = {0:0}\n",
    "        for i in nums:\n",
    "            cnt[i] += 1\n",
    "        for i,ch in enumerate(cnt):\n",
    "            hash_T[i+1] = hash_T[i] + ch\n",
    "        return [hash_T[i] for i in nums]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        new_list = []\n",
    "        for i in range(len(nums)):\n",
    "\n",
    "            count = 0\n",
    "            for j in range(len(nums)):\n",
    "                if nums[i]>nums[j]:\n",
    "                    count +=1\n",
    "            new_list.append(count)\n",
    "\n",
    "        return new_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        a=sorted(nums)\n",
    "        b = []\n",
    "        for i in range(len(nums)):\n",
    "            b.append(a.index(nums[i]))\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        # 两层 for 循环，时间复杂度为 O(N^2)\n",
    "        # 优化：1. 先排序，2. 用哈希表记录每个元素对应的下表。对于相同的元素，只记录最左边的下标，因此若当前元素已经在哈希表中被记录过了，则跳过。\n",
    "        # 优化后：先排序再遍历，时间复杂度为 O(nlogn)\n",
    "        hashmap = dict()\n",
    "        res = nums[:]\n",
    "        res.sort()\n",
    "        for idx, num in enumerate(res):\n",
    "            if num not in hashmap.keys():\n",
    "                hashmap[num] = idx\n",
    "\n",
    "        for i, x in enumerate(nums):\n",
    "            res[i] = hashmap[x]\n",
    "        return res\n",
    "        # res = nums[:]\n",
    "        # hash = dict()\n",
    "        # res.sort() # 从小到大排序之后，元素下标就是小于当前数字的数字\n",
    "        # for i, num in enumerate(res):\n",
    "        #     if num  not in hash.keys(): # 遇到了相同的数字，那么不需要更新该 number 的情况\n",
    "        #         hash[num] = i       \n",
    "        # for i, num in enumerate(nums):\n",
    "        #     res[i] = hash[num]\n",
    "        # return res\n",
    "        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        sorted_nums = sorted([(v, i) for i, v in enumerate(nums)])\n",
    "\n",
    "        result = [0] * n\n",
    "        prev_count = 0\n",
    "        for i in range(1, n):\n",
    "            if sorted_nums[i][0] != sorted_nums[i-1][0]:\n",
    "                prev_count = i\n",
    "            result[sorted_nums[i][1]] = prev_count\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        place = [0] * 101\n",
    "        output = []\n",
    "\n",
    "        for n in nums:\n",
    "            place[n] += 1  # 把从0 - 100的所有数的个数都数出来了。\n",
    "\n",
    "        lessthan = []  # 把从0 - 100的所有数的比它小的数的个数都列出来。\n",
    "        temp = 0  # 其实就是刚才的place数组的累加\n",
    "        for p in place:\n",
    "            lessthan.append(temp)\n",
    "            temp += p\n",
    "\n",
    "        for n in nums:  # 最后对应nums把lessthan的值掏出来作为输出。\n",
    "            output.append(lessthan[n])\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        res = nums[:]\n",
    "        hash = dict()\n",
    "        res.sort() # 从小到大排序之后，元素下标就是小于当前数字的数字\n",
    "        for i, num in enumerate(res):\n",
    "            if num  not in hash.keys(): # 遇到了相同的数字，那么不需要更新该 number 的情况\n",
    "                hash[num] = i       \n",
    "        for i, num in enumerate(nums):\n",
    "            res[i] = hash[num]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        return [sum(i < n for i in nums) for n in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        sort_nums = sorted(nums)\n",
    "        hash = {}\n",
    "        for x,y in enumerate(sort_nums):\n",
    "            if y not in hash:\n",
    "                hash[y] = x\n",
    "        return [hash[n] for n in nums]\n",
    "\n",
    "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        s_nums = sorted(nums)\n",
    "        res = [0] * len(nums)\n",
    "        for k in range(len(nums)):\n",
    "            n = nums[k]\n",
    "            id = s_nums.index(n)\n",
    "            i = id\n",
    "            while i >= 0 and s_nums[i] == n:\n",
    "                i -= 1\n",
    "            res[k] = i + 1 if i >= 0 else 0 \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        l = [0] * len(nums)\n",
    "        \n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                if i != j and nums[j] < nums[i]:\n",
    "                    l[i] += 1\n",
    "        return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        sort_nums = sorted(nums)\n",
    "        result = [0] * len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            result[i] = sort_nums.index(nums[i])\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        place = [0] * 101\n",
    "        output = []\n",
    "\n",
    "        for n in nums:\n",
    "            place[n] += 1  # 把从0 - 100的所有数的个数都数出来了。\n",
    "\n",
    "        lessthan = []  # 把从0 - 100的所有数的比它小的数的个数都列出来。\n",
    "        temp = 0  # 其实就是刚才的place数组的累加\n",
    "        for p in place:\n",
    "            lessthan.append(temp)\n",
    "            temp += p\n",
    "\n",
    "        for n in nums:  # 最后对应nums把lessthan的值掏出来作为输出。\n",
    "            output.append(lessthan[n])\n",
    "\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        steps = 0\n",
    "        reslist = []\n",
    "        length = len(nums)\n",
    "        while steps < length:\n",
    "            resnum = 0\n",
    "            for i in range(0,length):\n",
    "                if nums[i] < nums[steps]:\n",
    "                    resnum += 1\n",
    "            reslist.append(resnum)\n",
    "            steps += 1\n",
    "        return reslist\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        for i in nums:\n",
    "            sum = 0\n",
    "            for j in nums:\n",
    "                if i > j:\n",
    "                    sum += 1\n",
    "            result.append(sum)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        cnt = [0] * (max(nums) + 1)\n",
    "        hm = {}\n",
    "        for i, num in enumerate(nums):\n",
    "            cnt[num] += 1\n",
    "        for i, num in enumerate(nums.copy()):\n",
    "            nums[i] = sum(cnt[:num])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        sort_nums = sorted(nums)\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            res.append(sort_nums.index(num))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        place = [0] * 101\n",
    "        output = []\n",
    "\n",
    "        for i in nums:\n",
    "            place[i] += 1\n",
    "        # print(place)\n",
    "        \n",
    "        lessthan = []\n",
    "        temp = 0\n",
    "        for j in place:\n",
    "            lessthan.append(temp)\n",
    "            temp += j\n",
    "        # print(lessthan)\n",
    "\n",
    "        for n in nums:\n",
    "            output.append(lessthan[n])\n",
    "        return output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        x = nums.copy()\n",
    "        x.sort()\n",
    "        hm = {}\n",
    "        for i, num in enumerate(x):\n",
    "            if num not in hm:\n",
    "                hm[num] = i\n",
    "        for i, num in enumerate(nums.copy()):\n",
    "            nums[i] = hm[num]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        cnt = [0] * (max(nums) + 1)\n",
    "        hm = {}\n",
    "        for i, num in enumerate(nums):\n",
    "            cnt[num] += 1\n",
    "        for i, num in enumerate(nums.copy()):\n",
    "            nums[i] = sum(cnt[:num])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        for i in range(len(nums)):\n",
    "            count = 0\n",
    "            for j in range(len(nums)):\n",
    "                if j != i and nums[j] < nums[i]:\n",
    "                    count += 1\n",
    "            ans.append(count)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        ret=[]\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            ret.append(0)\n",
    "            for j in range(n):\n",
    "                if j!=i and  nums[j] < nums[i]:\n",
    "                    ret[i]+=1\n",
    "        return ret "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        s_nums = sorted(nums)\n",
    "        res = [0] * len(nums)\n",
    "        for k in range(len(nums)):\n",
    "            n = nums[k]\n",
    "            res[k] = s_nums.index(n)      \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        return [sum(x < i for x in nums) for i in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        for i, a in enumerate(nums):\n",
    "            cnt = 0\n",
    "            for j, b in enumerate(nums):\n",
    "                if i != j and a > b:\n",
    "                    cnt += 1\n",
    "            ans.append(cnt)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        s=[]\n",
    "        for i in nums:\n",
    "            n=0\n",
    "            for j in nums:\n",
    "                if j==i:\n",
    "                    continue\n",
    "                if j<i:\n",
    "                    n+=1\n",
    "            s.append(n)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        s=[]\n",
    "        for i in nums:\n",
    "            n=0\n",
    "            for j in nums:\n",
    "                if j==i:\n",
    "                    continue\n",
    "                if j<i:\n",
    "                    n+=1\n",
    "            s.append(n)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        s=[]\n",
    "        for i in nums:\n",
    "            n=0\n",
    "            for j in nums:\n",
    "                if j==i:\n",
    "                    continue\n",
    "                if j<i:\n",
    "                    n+=1\n",
    "            s.append(n)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        \n",
    "        n1=sorted(nums)\n",
    "        dict1 = {}\n",
    "        for i in range(len(n1)):\n",
    "            if n1[i] not in dict1:\n",
    "                dict1[n1[i]] = i\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            res.append(dict1[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n=[]\n",
    "        for i in nums:\n",
    "            sum = 0\n",
    "            for j in nums:\n",
    "                if i > j :\n",
    "                    sum += 1\n",
    "            n.append(sum)\n",
    "        return n\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        result = []\n",
    "        for i,x in enumerate(nums):\n",
    "            c = 0\n",
    "            for j,y in enumerate(nums):\n",
    "                if y < x:\n",
    "                    c += 1\n",
    "            result.append(c)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        hm = {}\n",
    "        max_n = 0\n",
    "        for i, num in enumerate(nums):\n",
    "            max_n = max(max_n, num)\n",
    "            if v := hm.get(num):\n",
    "                hm[num] = v + 1\n",
    "            else:\n",
    "                hm[num] = 1\n",
    "        cnt = [0] * (max_n + 1) \n",
    "        for k, v in hm.items():\n",
    "            cnt[k] = v\n",
    "        for i, num in enumerate(nums.copy()):\n",
    "            nums[i] = sum(cnt[:num])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        count_list = [0] * len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                if i !=j and nums[i] > nums[j]:\n",
    "                    count_list[i] += 1\n",
    "        return count_list\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n1 = nums.copy()\n",
    "        n1.sort()\n",
    "        dict1 = {}\n",
    "        for i in range(len(n1)):\n",
    "            if n1[i] not in dict1:\n",
    "                dict1[n1[i]] = i\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            res.append(dict1[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        l=[]\n",
    "        for i in nums:\n",
    "            temp=0\n",
    "            for j in nums:\n",
    "              if i>j:\n",
    "                  temp+=1\n",
    "            l.append(temp)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        date_list = []\n",
    "        for date in nums:\n",
    "            count = 0\n",
    "            for date2 in nums:\n",
    "                if date > date2:\n",
    "                   count += 1\n",
    "            date_list.append(count)\n",
    "\n",
    "        return date_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n1=sorted(nums)\n",
    "        dict1 = {}\n",
    "        for i in range(len(n1)):\n",
    "            if n1[i] not in dict1:\n",
    "                dict1[n1[i]] = i\n",
    "        return [dict1[num] for num in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        res = [0]*len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                if nums[j] < nums[i]:\n",
    "                    res[i] += 1\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        nums2 = []\n",
    "        nums3 = sorted(nums)\n",
    "        for i in nums:\n",
    "            nums2.append(nums3.index(i))\n",
    "        return nums2\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        return [ sum(1 for j in nums if j < i) for i in nums ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        place = [0]*101\n",
    "        output = []\n",
    "\n",
    "        for n in nums:\n",
    "            place[n] += 1\n",
    "\n",
    "        lessthan = []\n",
    "        temp = 0\n",
    "\n",
    "        for p in place:\n",
    "            lessthan.append(temp)\n",
    "            temp += p\n",
    "\n",
    "        for n in nums:\n",
    "            output.append(lessthan[n])\n",
    "\n",
    "        return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        \"\"\"\n",
    "        mark: 看的别人的桶思想\n",
    "        1.定义一排桶res[100],期中res[i]里存放数组n出翔的次数\n",
    "        2.遍历nums,初始化桶数组\n",
    "        3.累加处理桶数组res，使得res[i]表示比i小的数字的个数\n",
    "        4.遍历最后的元素nums, 取出最后对应的桶的元素结果\n",
    "        # \"\"\"\n",
    "        # res = [0]*101\n",
    "\n",
    "        # # 对于出现的元素进行累积 初始化桶\n",
    "        # for i in nums:\n",
    "        #     res[i] = res[i] + 1\n",
    "\n",
    "        # # 累加处理计算桶,res[i]表示比i小的数字的个数\n",
    "        # cnt = 0\n",
    "        # for i in range(0, 101, 1):\n",
    "        #     t = res[i]\n",
    "        #     res[i] = cnt\n",
    "        #     cnt = cnt + t\n",
    "\n",
    "        # ret = []\n",
    "        # for i in nums:\n",
    "        #     ret.append(res[i])\n",
    "\n",
    "        # return ret\n",
    "        d = {}\n",
    "        for i in nums:\n",
    "            d[i] = d.get(i, 0) + 1\n",
    "\n",
    "        cnt = 0\n",
    "        for i in range(0, 101, 1):\n",
    "            t = d.get(i, 0)\n",
    "            d[i] = cnt \n",
    "            cnt = cnt + t\n",
    "\n",
    "        ret = []\n",
    "        for i in nums:\n",
    "            ret.append(d[i])\n",
    "\n",
    "        return ret       \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        res=[]\n",
    "        t=sorted(nums,reverse=True)\n",
    "        for i in nums:\n",
    "            flag=False\n",
    "            for j in range(len(t)):\n",
    "                if t[j]<i:\n",
    "                    res.append(len(t)-j)\n",
    "                    flag=True\n",
    "                    break\n",
    "            if not flag:\n",
    "                res.append(0)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        ct_hash = {i:0 for i in range(101)}\n",
    "        res = list()\n",
    "        for num in nums:\n",
    "            ct_hash[num]+=1\n",
    "        cum = 0\n",
    "        for idx in range(101):\n",
    "            temp = ct_hash[idx]\n",
    "            ct_hash[idx] = cum\n",
    "            cum += temp\n",
    "        for num in nums:\n",
    "            res.append(ct_hash[num])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        l = [0] * len(nums)\n",
    "        \n",
    "        for i in range(len(nums)):\n",
    "            for j in range(len(nums)):\n",
    "                if i != j and nums[j] < nums[i]:\n",
    "                    l[i] += 1\n",
    "        return l\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        sorted_nums = sorted(nums)\n",
    "        hash_dict = {}\n",
    "        for i in range(len(sorted_nums)-1, -1, -1):\n",
    "            hash_dict[sorted_nums[i]] = i\n",
    "\n",
    "        res = []\n",
    "        for n in nums:\n",
    "            res.append(hash_dict[n])\n",
    "        \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n1 = nums.copy()\n",
    "        n1.sort()\n",
    "        dict1 = {}\n",
    "        for i in range(len(n1)):\n",
    "            if n1[i] not in dict1:\n",
    "                dict1[n1[i]] = i\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            res.append(dict1[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        n1 = nums.copy()\n",
    "        n1.sort()\n",
    "        dict1 = {}\n",
    "        for i in range(len(n1)):\n",
    "            if n1[i] not in dict1:\n",
    "                dict1[n1[i]] = i\n",
    "        res = []\n",
    "        for num in nums:\n",
    "            res.append(dict1[num])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        res = nums[:]\n",
    "        hash = dict()\n",
    "        res.sort()\n",
    "        for i, num in enumerate(res):\n",
    "            if num not in hash.keys():\n",
    "                hash[num] = i\n",
    "        for i, num in enumerate(nums):\n",
    "            res[i] = hash[num]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:\n",
    "        nums_sort = sorted(nums)\n",
    "        item_num_dic = {}\n",
    "        item_num_dic[nums_sort[0]] = 0\n",
    "        res = []\n",
    "        for i in range(1,len(nums_sort),1):\n",
    "            if nums_sort[i]==nums_sort[i-1]:\n",
    "                print(i-1)\n",
    "                item_num_dic[nums_sort[i]] = item_num_dic.get(nums_sort[i-1])\n",
    "            else:\n",
    "                item_num_dic[nums_sort[i]] = i\n",
    "        print(item_num_dic)\n",
    "        for x in nums:\n",
    "            res.append(item_num_dic.get(x))\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
